<template>
  <div class="news-information">
    <!-- 页面头部 -->
    <div class="news-header">
      <div class="header-container">
        <div class="header-content wow fadeInUp">
          <h1 class="page-title">新闻动态</h1>
          <p class="page-subtitle">了解公司最新动态和行业资讯</p>
        </div>
      </div>
    </div>

    <!-- 新闻分类和搜索 -->
    <div class="news-filters">
      <div class="filters-container">
        <div class="filter-tabs wow fadeInUp">
          <button
            v-for="category in categories"
            :key="category.key"
            :class="['tab-btn', { active: activeCategory === category.key }]"
            @click="setActiveCategory(category.key)"
          >
            {{ category.name }}
          </button>
        </div>

        <div class="search-box wow fadeInUp" data-wow-delay="0.1s">
          <div class="search-input-wrapper">
            <input
              type="text"
              v-model="searchKeyword"
              placeholder="搜索新闻..."
              class="search-input"
              @input="handleSearch"
            />
            <i class="glyphicon glyphicon-search search-icon"></i>
          </div>
        </div>
      </div>
    </div>

    <!-- 新闻列表 -->
    <div class="news-content">
      <div class="content-container">
        <div class="news-grid">
          <div
            v-for="news in paginatedNews"
            :key="news.id"
            class="news-card wow fadeInUp"
            :data-wow-delay="`${(news.id % 3) * 0.1}s`"
            @click="goToNewsDetail(news.id)"
          >
            <div class="news-image" v-if="news.thumbnail">
              <img :src="news.thumbnail" :alt="news.title" />
              <div class="news-overlay">
                <span class="read-more">阅读更多</span>
              </div>
            </div>

            <div class="news-content-wrapper">
              <div class="news-meta">
                <span class="news-category">{{ news.category }}</span>
                <span class="news-date">{{ formatDate(news.publishDate) }}</span>
              </div>

              <h3 class="news-title">{{ news.title }}</h3>
              <p class="news-summary">{{ news.summary }}</p>

              <div class="news-footer">
                <div class="news-stats">
                  <span class="news-views">
                    <i class="glyphicon glyphicon-eye-open"></i>
                    {{ news.views }}
                  </span>
                  <span class="news-comments" v-if="news.comments">
                    <i class="glyphicon glyphicon-comment"></i>
                    {{ news.comments }}
                  </span>
                </div>
                <button class="read-more-btn">
                  阅读全文
                  <i class="glyphicon glyphicon-arrow-right"></i>
                </button>
              </div>
            </div>
          </div>
        </div>

        <!-- 没有找到新闻 -->
        <div v-if="filteredNews.length === 0" class="no-news">
          <div class="no-news-content">
            <i class="glyphicon glyphicon-file"></i>
            <h3>暂无相关新闻</h3>
            <p>{{ searchKeyword ? '没有找到符合条件的新闻' : '该分类下暂无新闻' }}</p>
          </div>
        </div>

        <!-- 分页组件 -->
        <div v-if="totalPages > 1" class="pagination-wrapper wow fadeInUp">
          <nav class="pagination">
            <button
              class="pagination-btn prev"
              :disabled="currentPage === 1"
              @click="changePage(currentPage - 1)"
            >
              <i class="glyphicon glyphicon-chevron-left"></i>
              上一页
            </button>

            <div class="page-numbers">
              <button
                v-for="page in visiblePages"
                :key="page"
                :class="['page-btn', { active: page === currentPage }]"
                @click="changePage(page)"
              >
                {{ page }}
              </button>
            </div>

            <button
              class="pagination-btn next"
              :disabled="currentPage === totalPages"
              @click="changePage(currentPage + 1)"
            >
              下一页
              <i class="glyphicon glyphicon-chevron-right"></i>
            </button>
          </nav>

          <div class="pagination-info">
            共 {{ filteredNews.length }} 条新闻，第 {{ currentPage }} / {{ totalPages }} 页
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="NewsInformation">
import WOW from 'wow.js'
import { onMounted, ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

// 响应式数据
const activeCategory = ref('all')
const searchKeyword = ref('')
const currentPage = ref(1)
const pageSize = ref(6)

// 新闻分类
const categories = ref([
  { key: 'all', name: '全部新闻' },
  { key: 'company', name: '公司新闻' },
  { key: 'industry', name: '行业动态' },
  { key: 'product', name: '产品更新' },
  { key: 'event', name: '活动资讯' }
])

// 新闻数据
const newsList = ref([
  {
    id: '001',
    title: '杭州玄鸽科技荣获"2024年度最佳创新企业"奖项',
    summary: '在科技创新大会上，我公司凭借在AI和大数据领域的杰出贡献获得表彰，这一殊荣充分体现了公司的技术实力。',
    category: '公司新闻',
    categoryKey: 'company',
    publishDate: '2024-03-15',
    views: 1253,
    comments: 18,
    thumbnail: '/src/assets/img/news-1.jpg',
    featured: true
  },
  {
    id: '002',
    title: '玄鸽科技完成A轮融资，继续领跑科技创新',
    summary: '公司宣布完成A轮融资，本轮融资将主要用于产品研发、团队扩张和市场推广，进一步巩固行业领先地位。',
    category: '公司新闻',
    categoryKey: 'company',
    publishDate: '2024-03-10',
    views: 2891,
    comments: 35,
    thumbnail: '/src/assets/img/news-2.jpg'
  },
  {
    id: '003',
    title: 'GuDB数据库引擎性能测试报告发布',
    summary: '最新测试报告显示，GuDB在保持2MB小体积的同时，实现了100K QPS的卓越性能，引起业界广泛关注。',
    category: '产品更新',
    categoryKey: 'product',
    publishDate: '2024-03-08',
    views: 1845,
    comments: 24,
    thumbnail: '/src/assets/img/news-3.jpg'
  },
  {
    id: '004',
    title: '创业者之家平台用户突破10万大关',
    summary: '平台自上线以来，凭借AI驱动的商业计划书生成、智能化市场分析等创新功能，受到广大创业者青睐。',
    category: '产品更新',
    categoryKey: 'product',
    publishDate: '2024-03-05',
    views: 1567,
    comments: 42,
    thumbnail: '/src/assets/img/news-4.jpg'
  },
  {
    id: '005',
    title: '2024年科技趋势展望：AI与大数据的融合发展',
    summary: '行业专家深入分析了人工智能与大数据技术的发展趋势，预测未来几年将是技术深度融合的关键时期。',
    category: '行业动态',
    categoryKey: 'industry',
    publishDate: '2024-03-03',
    views: 3245,
    comments: 67,
    thumbnail: '/src/assets/img/news-5.jpg',
    featured: true
  },
  {
    id: '006',
    title: '数字化转型加速，企业如何把握机遇',
    summary: '随着数字化转型浪潮的推进，企业需要重新审视自身的技术架构和业务模式，抓住转型机遇。',
    category: '行业动态',
    categoryKey: 'industry',
    publishDate: '2024-03-01',
    views: 2156,
    comments: 31,
    thumbnail: '/src/assets/img/news-6.jpg'
  },
  {
    id: '007',
    title: '玄鸽科技参加杭州科技博览会',
    summary: '公司携最新产品和解决方案亮相博览会，展示了在智能化平台和数据库技术方面的创新成果。',
    category: '活动资讯',
    categoryKey: 'event',
    publishDate: '2024-02-28',
    views: 967,
    comments: 15,
    thumbnail: '/src/assets/img/news-7.jpg'
  },
  {
    id: '008',
    title: '云计算市场分析：机遇与挑战并存',
    summary: '深度解析当前云计算市场格局，分析技术发展趋势和市场机遇，为企业决策提供参考。',
    category: '行业动态',
    categoryKey: 'industry',
    publishDate: '2024-02-25',
    views: 1789,
    comments: 23,
    thumbnail: '/src/assets/img/news-8.jpg'
  },
  {
    id: '009',
    title: '箱野盲盒商城正式上线，重新定义盲盒经济',
    summary: '创新的盲盒电商平台正式发布，通过循环经济模式，将滞留包裹转化为惊喜盲盒，开创全新商业模式。',
    category: '产品更新',
    categoryKey: 'product',
    publishDate: '2024-02-22',
    views: 2345,
    comments: 56,
    thumbnail: '/src/assets/img/news-9.jpg'
  }
])

// 筛选后的新闻
const filteredNews = computed(() => {
  let filtered = newsList.value

  // 按分类筛选
  if (activeCategory.value !== 'all') {
    filtered = filtered.filter(news => news.categoryKey === activeCategory.value)
  }

  // 按关键词搜索
  if (searchKeyword.value.trim()) {
    const keyword = searchKeyword.value.toLowerCase().trim()
    filtered = filtered.filter(news =>
      news.title.toLowerCase().includes(keyword) ||
      news.summary.toLowerCase().includes(keyword) ||
      news.category.toLowerCase().includes(keyword)
    )
  }

  return filtered
})

// 分页相关计算
const totalPages = computed(() => Math.ceil(filteredNews.value.length / pageSize.value))

const paginatedNews = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return filteredNews.value.slice(start, end)
})

const visiblePages = computed(() => {
  const pages = []
  const total = totalPages.value
  const current = currentPage.value

  if (total <= 7) {
    for (let i = 1; i <= total; i++) {
      pages.push(i)
    }
  } else {
    if (current <= 4) {
      for (let i = 1; i <= 5; i++) {
        pages.push(i)
      }
      pages.push('...')
      pages.push(total)
    } else if (current >= total - 3) {
      pages.push(1)
      pages.push('...')
      for (let i = total - 4; i <= total; i++) {
        pages.push(i)
      }
    } else {
      pages.push(1)
      pages.push('...')
      for (let i = current - 1; i <= current + 1; i++) {
        pages.push(i)
      }
      pages.push('...')
      pages.push(total)
    }
  }

  return pages
})

// 方法
function setActiveCategory(category) {
  activeCategory.value = category
  currentPage.value = 1 // 重置到第一页
}

function handleSearch() {
  currentPage.value = 1 // 重置到第一页
}

function changePage(page) {
  if (page >= 1 && page <= totalPages.value) {
    currentPage.value = page
    // 滚动到新闻列表顶部
    document.querySelector('.news-content').scrollIntoView({
      behavior: 'smooth',
      block: 'start'
    })
  }
}

function goToNewsDetail(newsId) {
  router.push(`/news/${newsId}`)
}

function formatDate(dateString) {
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

// 监听筛选条件变化，重置页码
watch([activeCategory, searchKeyword], () => {
  currentPage.value = 1
})

onMounted(() => {
  const wow = new WOW({
    boxClass: 'wow',
    animateClass: 'animated',
    offset: 0,
    mobile: true,
    live: true
  })
  wow.init()
})
</script>

<style scoped>
/* 新闻信息页面样式 - 苹果风格 */
.news-information {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Helvetica Neue', Arial, sans-serif;
  background: #ffffff;
  color: #1d1d1f;
  min-height: 100vh;
}

/* 页面头部 */
.news-header {
  padding: 80px 0 60px;
  background: linear-gradient(135deg, #f5f5f7 0%, #ffffff 100%);
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.header-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-content {
  text-align: center;
}

.page-title {
  font-size: clamp(32px, 4vw, 48px);
  font-weight: 700;
  color: #1d1d1f;
  margin: 0 0 16px 0;
  letter-spacing: -0.02em;
  line-height: 1.08;
}

.page-subtitle {
  font-size: 19px;
  font-weight: 400;
  color: #86868b;
  margin: 0;
  line-height: 1.42;
}

/* 筛选器 */
.news-filters {
  padding: 40px 0;
  background: #f5f5f7;
  border-bottom: 1px solid rgba(0, 0, 0, 0.06);
}

.filters-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.filter-tabs {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tab-btn {
  padding: 10px 20px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: #ffffff;
  color: #515154;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tab-btn:hover {
  border-color: rgba(0, 113, 227, 0.3);
  color: #0071e3;
}

.tab-btn.active {
  background: #0071e3;
  color: #ffffff;
  border-color: #0071e3;
}

.search-box {
  position: relative;
  min-width: 280px;
}

.search-input-wrapper {
  position: relative;
}

.search-input {
  width: 100%;
  padding: 12px 20px 12px 44px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 20px;
  font-size: 14px;
  background: #ffffff;
  transition: all 0.2s ease;
}

.search-input:focus {
  outline: none;
  border-color: #0071e3;
  box-shadow: 0 0 0 4px rgba(0, 113, 227, 0.1);
}

.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #86868b;
  font-size: 14px;
}

/* 新闻内容 */
.news-content {
  padding: 80px 0;
}

.content-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.news-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
  gap: 32px;
  margin-bottom: 60px;
}

.news-card {
  background: #ffffff;
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  cursor: pointer;
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.news-card:hover {
  transform: translateY(-8px) translateZ(0);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.12);
  border-color: rgba(0, 113, 227, 0.1);
}

.news-image {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.news-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.6s ease;
}

.news-card:hover .news-image img {
  transform: scale(1.05);
}

.news-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.news-card:hover .news-overlay {
  opacity: 1;
}

.news-overlay .read-more {
  color: #ffffff;
  font-size: 16px;
  font-weight: 500;
  background: rgba(0, 113, 227, 0.9);
  padding: 10px 20px;
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

.news-content-wrapper {
  padding: 24px;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.news-category {
  background: rgba(0, 113, 227, 0.1);
  color: #0071e3;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.news-date {
  font-size: 13px;
  color: #86868b;
}

.news-title {
  font-size: 18px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0 0 12px 0;
  line-height: 1.3;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.news-summary {
  font-size: 14px;
  color: #515154;
  line-height: 1.5;
  margin: 0 0 20px 0;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.news-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  padding-top: 16px;
}

.news-stats {
  display: flex;
  gap: 16px;
}

.news-views,
.news-comments {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #86868b;
}

.read-more-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: #0071e3;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.read-more-btn:hover {
  color: #0077ed;
  transform: translateX(2px);
}

/* 无新闻状态 */
.no-news {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
  text-align: center;
}

.no-news-content i {
  font-size: 48px;
  color: #86868b;
  margin-bottom: 20px;
}

.no-news-content h3 {
  font-size: 20px;
  font-weight: 600;
  color: #1d1d1f;
  margin: 0 0 12px 0;
}

.no-news-content p {
  font-size: 16px;
  color: #86868b;
  margin: 0;
}

/* 分页 */
.pagination-wrapper {
  text-align: center;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.pagination-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 16px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: #ffffff;
  color: #515154;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.pagination-btn:hover:not(:disabled) {
  border-color: #0071e3;
  color: #0071e3;
}

.pagination-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-numbers {
  display: flex;
  gap: 4px;
}

.page-btn {
  width: 40px;
  height: 40px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  background: #ffffff;
  color: #515154;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-btn:hover {
  border-color: #0071e3;
  color: #0071e3;
}

.page-btn.active {
  background: #0071e3;
  border-color: #0071e3;
  color: #ffffff;
}

.pagination-info {
  font-size: 14px;
  color: #86868b;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .news-grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 24px;
  }
}

@media (max-width: 768px) {
  .news-header {
    padding: 60px 0 40px;
  }

  .header-container,
  .filters-container,
  .content-container {
    padding: 0 16px;
  }

  .filters-container {
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
  }

  .filter-tabs {
    justify-content: center;
  }

  .search-box {
    min-width: auto;
  }

  .news-content {
    padding: 60px 0;
  }

  .news-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .news-card {
    border-radius: 16px;
  }

  .news-content-wrapper {
    padding: 20px;
  }

  .pagination {
    gap: 4px;
  }

  .pagination-btn {
    padding: 8px 12px;
    font-size: 13px;
  }

  .page-btn {
    width: 36px;
    height: 36px;
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .filter-tabs {
    gap: 6px;
  }

  .tab-btn {
    padding: 8px 16px;
    font-size: 13px;
  }

  .news-image {
    height: 160px;
  }

  .news-footer {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .pagination {
    justify-content: space-between;
  }

  .page-numbers {
    order: 2;
    flex: 1;
    justify-content: center;
  }

  .pagination-btn.prev {
    order: 1;
  }

  .pagination-btn.next {
    order: 3;
  }
}
</style>